package com.kcwl.carrier.infrastructure.repository.persostence;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.entity.VehicleInfo;
import com.kcwl.carrier.domain.entity.VehicleInfoListVo;
import com.kcwl.carrier.domain.entity.VehicleInfoVo;
import com.kcwl.carrier.domain.repository.IVehicleInfoRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.VehicleInfoMapper;
import com.kcwl.carrier.infrastructure.repository.po.VehicleInfoPO;
import com.kcwl.carrier.interfaces.dto.BaseInfoDTO;
import com.kcwl.carrier.interfaces.dto.OrderVehicleDTO;
import com.kcwl.carrier.interfaces.query.MobileQuery;
import com.kcwl.carrier.interfaces.query.OrderVehicleQuery;
import com.kcwl.carrier.interfaces.query.UserIdQuery;
import com.kcwl.carrier.interfaces.query.VehicleQuery;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.CollectionUtil;
import com.kcwl.framework.utils.KcBeanConverter;
import com.kcwl.framework.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 刘旭超
 * @date 2022年07月11日 16:33
 * @Description
 */
@Service
public class VehicleInfoRepositoryImpl extends BaseRepositoryImpl<VehicleInfoMapper, VehicleInfoPO> implements IVehicleInfoRepository {

    @Resource
    private VehicleInfoMapper vehicleInfoMapper;

    @Override
    public Long insert(VehicleInfo vehicleInfo) {
        vehicleInfo.setSubmitTime(new Date());
        VehicleInfoPO po = KcBeanConverter.toBean(vehicleInfo, VehicleInfoPO.class);
        po.setVehicleSize(vehicleInfo.getSize());
        po.setVehicleAddress(vehicleInfo.getAddress());
        po.setVehicleOwner(vehicleInfo.getOwner());
        po.setVehicleUsage(vehicleInfo.getUsage());
        vehicleInfoMapper.insert(po);
        return po.getId();
    }

    @Override
    public int updateById(VehicleInfo vehicleInfo) {
        VehicleInfoPO po = KcBeanConverter.toBean(vehicleInfo, VehicleInfoPO.class);
        po.setModifiedTime(new Date());
        po.setVehicleSize(vehicleInfo.getSize());
        po.setVehicleAddress(vehicleInfo.getAddress());
        po.setVehicleOwner(vehicleInfo.getOwner());
        po.setVehicleUsage(vehicleInfo.getUsage());
        return vehicleInfoMapper.updateById(po);
    }

    @Override
    public List<VehicleInfo> selectList(VehicleInfo vehicleInfo) {
        QueryWrapper<VehicleInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq(Objects.nonNull(vehicleInfo.getIsDel()), "is_del", vehicleInfo.getIsDel());
        wrapper.eq(Objects.nonNull(vehicleInfo.getCarrierId()), "carrier_id", vehicleInfo.getCarrierId());
        wrapper.eq(StringUtil.isNotBlank(vehicleInfo.getPlateNumber()), "plate_number", vehicleInfo.getPlateNumber());
        List<VehicleInfoPO> pos = vehicleInfoMapper.selectList(wrapper);
        ArrayList<VehicleInfo> infos = new ArrayList<>();
        pos.forEach(po -> {
            VehicleInfo info = KcBeanConverter.toBean(po, VehicleInfo.class);
            info.setUsage(po.getVehicleUsage());
            info.setAddress(po.getVehicleAddress());
            info.setOwner(po.getVehicleOwner());
            info.setSize(po.getVehicleSize());
            infos.add(info);
        });
        return infos;
    }

    @Override
    public int updateVehicleCategory(VehicleInfo vehicleInfo) {
        return vehicleInfoMapper.updateVehicleCategory(KcBeanConverter.toBean(vehicleInfo, VehicleInfoPO.class));
    }

    @Override
    public boolean countTrailerBindNum(Map<String, Object> params) {
        return vehicleInfoMapper.countTrailerBindNum(params) > 0;
    }

    @Override
    public Integer selectCount(VehicleInfo countVehicleInfo) {
        QueryWrapper<VehicleInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", countVehicleInfo.getIsDel());
        wrapper.eq("carrier_id", countVehicleInfo.getCarrierId());
        return vehicleInfoMapper.selectCount(wrapper);
    }

    @Override
    public Integer getVehicleCount(VehicleQuery query) {
        return vehicleInfoMapper.getVehicleCount(query);
    }

    @Override
    public KcPage<VehicleInfoVo> getVehiclePage(VehicleQuery query) {
        IPage<VehicleInfoVo> vehiclePage = vehicleInfoMapper.getVehiclePage(new Page<>(query.getCurPagerNo(), query.getPageSize()), query);
        return new KcPage<>(vehiclePage.getTotal(), vehiclePage.getCurrent(), vehiclePage.getSize(), vehiclePage.getRecords());
    }

    @Override
    public Integer updateVehicleTrail(Long id) {
        UpdateWrapper<VehicleInfoPO> wrapper = new UpdateWrapper<>();
        wrapper.set("trailer_id", null);
        wrapper.eq("id", id);
        VehicleInfoPO vehicleInfoPO = new VehicleInfoPO();
        vehicleInfoPO.setId(id);
        vehicleInfoPO.setTrailerId(null);
        return vehicleInfoMapper.update(vehicleInfoPO, wrapper);
    }

    @Override
    public List<VehicleInfo> getUnBindVehicles(UserIdQuery query) {
        List<VehicleInfoPO> pos = vehicleInfoMapper.getUnBindVehicles(query);
        ArrayList<VehicleInfo> infos = new ArrayList<>();
        pos.forEach(po -> {
            VehicleInfo info = KcBeanConverter.toBean(po, VehicleInfo.class);
            info.setUsage(po.getVehicleUsage());
            info.setAddress(po.getVehicleAddress());
            info.setOwner(po.getVehicleOwner());
            info.setSize(po.getVehicleSize());
            infos.add(info);
        });
        return infos;
    }

    @Override
    public List<VehicleInfo> queryByIds(List<Long> ids) {
        QueryWrapper<VehicleInfoPO> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        List<VehicleInfoPO> pos = vehicleInfoMapper.selectList(wrapper);
        ArrayList<VehicleInfo> infos = new ArrayList<>();
        pos.forEach(po -> {
            VehicleInfo info = KcBeanConverter.toBean(po, VehicleInfo.class);
            info.setUsage(po.getVehicleUsage());
            info.setAddress(po.getVehicleAddress());
            info.setOwner(po.getVehicleOwner());
            info.setSize(po.getVehicleSize());
            infos.add(info);
        });
        return infos;
    }

    @Override
    public VehicleInfoVo getHistoryVehicle(VehicleInfo vehicleInfo) {
        QueryWrapper<VehicleInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(vehicleInfo.getIsDel()), "is_del", vehicleInfo.getIsDel());
        wrapper.eq(ObjectUtil.isNotEmpty(vehicleInfo.getCarrierId()), "carrier_id", vehicleInfo.getCarrierId());
        wrapper.eq(ObjectUtil.isNotEmpty(vehicleInfo.getPlateNumber()), "plate_number", vehicleInfo.getPlateNumber());
        wrapper.eq(ObjectUtil.isNotEmpty(vehicleInfo.getAuthenticationStatus()), "authentication_status", vehicleInfo.getAuthenticationStatus());
        wrapper.orderByDesc("id");
        wrapper.last("limit 1");
        return KcBeanConverter.toBean(vehicleInfoMapper.selectOne(wrapper), VehicleInfoVo.class);
    }

    @Override
    public KcPage<VehicleInfoListVo> queryVehiclePage(VehicleQuery query) {
        IPage<VehicleInfoListVo> iPage = vehicleInfoMapper.queryVehiclePage(new Page<>(query.getCurPagerNo(), query.getPageSize()), query);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public List<VehicleInfoListVo> findAuthCarNum(List<Long> idList) {
        return vehicleInfoMapper.findAuthCarNum(idList);
    }

    @Override
    public void delTrailerBind(Long trailerId) {
        UpdateWrapper<VehicleInfoPO> wrapper = new UpdateWrapper<>();
        wrapper.eq("trailer_id", trailerId);
        wrapper.set("trailer_id", null);
        VehicleInfoPO po = new VehicleInfoPO();
        po.setTrailerId(null);
        vehicleInfoMapper.update(po, wrapper);
    }

    @Override
    public List<BaseInfoDTO> getBaseInfoByMobile(MobileQuery query) {
        return KcBeanConverter.toList(vehicleInfoMapper.getBaseInfoByMobile(query.getMobile()), BaseInfoDTO.class);
    }

    @Override
    public List<OrderVehicleDTO> getAllOrderVehicle(OrderVehicleQuery query) {
        return vehicleInfoMapper.getAllOrderVehicle(query);
    }

    @Override
    public KcPage<VehicleInfo> getAppropriateVehicles(VehicleQuery query) {
        QueryWrapper<VehicleInfoPO> wrapper = new QueryWrapper<>();
        wrapper.in(!CollectionUtil.isEmpty(query.getCarrierIds()), "carrier_id", query.getCarrierIds());
        wrapper.notIn(!CollectionUtil.isEmpty(query.getUnCarrierIds()), "carrier_id", query.getUnCarrierIds());
        wrapper.notIn(!CollectionUtil.isEmpty(query.getUnVehicleIds()), "id", query.getUnVehicleIds());
        wrapper.in(!CollectionUtil.isEmpty(query.getPlateNumbers()), "plate_number", query.getPlateNumbers());
        wrapper.eq("authentication_status", query.getAuthenticationStatus());
        wrapper.eq("is_del", query.getIsDel());
        Page<VehicleInfoPO> infoPOPage = vehicleInfoMapper.selectPage(new Page<>(query.getCurPagerNo(), query.getPageSize()), wrapper);
        return new KcPage<>(infoPOPage.getTotal(), infoPOPage.getCurrent(), infoPOPage.getSize(), infoPOPage.getRecords());
    }

    @Override
    public VehicleInfo queryByTrailerId(Long trailerId, Long carrierId) {
        QueryWrapper<VehicleInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq("trailer_id", trailerId);
        wrapper.eq("carrier_id", carrierId);
        wrapper.eq("is_del", YesNoEnum.NAY.getValue());
        return KcBeanConverter.toBean(vehicleInfoMapper.selectOne(wrapper), VehicleInfo.class);
    }

    @Override
    public Class<VehicleInfoPO> getPoClass() {
        return VehicleInfoPO.class;
    }
}
